<template>
	<view class="message">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				消息中心
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop clearfix" :style="{marginTop: pHeight + tHeight + 'px'}">
			<view class="left fl clearfix">
				<view class="mess fl" @click="qiehuan(1)">
					<view :class="[attrIndex==1?'active':'active1']">系统通知</view>
					<text class="dian" v-if="attrIndex == 1"></text>
				</view>
				<view class="mess fl" @click="qiehuan(2)">
					<view :class="[attrIndex==2?'active':'active2']">我的消息</view>
					<text class="dian" v-if="attrIndex == 2"></text>
				</view>
			</view>
			<view class="right fr" @click="qbduqu">
				全部已读
			</view>
		</view>
		<view class="nr" v-if="attrIndex == 1">
			<view class="list" v-for="(item,index) in tzList" :key="index">
				<view class="top">
					<view class="shang clearfix">
						<image :src="baseUrl + '/wximage/xitong.png'" class="fl" v-if="item.dqstatus == 1"></image>
						<image :src="baseUrl + '/wximage/xitong1.png'" class="fl" v-else ></image>
						<view class="title fl">{{item.title}}</view>
						<view class="time fr">{{item.shijian}}</view>
					</view>
					<view class="xia">
						{{item.riqi}}
					</view>
				</view>
				<view class="bottom clearfix" v-if="item.id != tzid" @click="chakantz(index)">
					<view class="name">查看详情</view>
					<image :src="baseUrl + '/wximage/right.png'" class="image"></image>
				</view>
				<view class="bottom clearfix" @click="tzid = ''" v-else>
					<view class="desc">{{item.description}}</view>
					<image :src="baseUrl + '/wximage/shang.png'" class="shouqi"></image>
				</view>
			</view>
		</view>
		<view class="empty" v-if="tzList.length == 0 && attrIndex == 1">
			<image :src="baseUrl + '/wximage/empty.png'"></image>
			<text>暂无相关数据</text>
		</view>
		
		<view class="nr" v-if="attrIndex == 2">
			<view class="list" v-for="(item,index) in xxList" :key="index">
				<view class="top">
					<view class="shang clearfix">
						<image :src="baseUrl + '/wximage/xiaoxi.png'" class="fl" v-if="item.dqstatus == 1"></image>
						<image :src="baseUrl + '/wximage/xiaoxi1.png'" class="fl" v-else ></image>
						<view class="title fl">{{item.title}}</view>
						<view class="time fr">{{item.shijian}}</view>
					</view>
					<view class="xia">
						{{item.riqi}}
					</view>
				</view>
				<view class="bottom clearfix" v-if="item.id != xxid" @click="chakanxx(index)">
					<view class="name">查看详情</view>
					<image :src="baseUrl + '/wximage/right.png'" class="image"></image>
				</view>
				<view class="bottom clearfix" @click="xxid = ''" v-else>
					<view class="desc">操作人名称:{{item.users_name}}</view>
					<view class="desc">操作人ID:{{item.users_id}}</view>
					<view class="desc">代理商名称:{{item.charger_agents_name}}</view>
					<view class="desc">商户名称:{{item.charger_bussiness_name}}</view>
					<view class="desc">员工名称:{{item.charger_yuangong_name}}</view>
					<view class="desc">门店名称:{{item.charger_station_name}}</view>
					<view class="desc">设备编码:{{item.charger_number}}</view>
					<view class="desc">充电宝编码:{{item.battery_sn}}</view>
					<view class="desc">槽位ID:{{item.caowei_id}}</view>
					<image :src="baseUrl + '/wximage/shang.png'" class="shouqi"></image>
				</view>
			</view>
		</view>
		<view class="empty" v-if="xxList.length == 0 && attrIndex == 2">
			<image :src="baseUrl + '/wximage/empty.png'"></image>
			<text>暂无相关数据</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				attrIndex: 1,
				tzid: '',
				tzList: [],
				tzpage: 1,
				tzpageSize: 10,
				tztotal: 0,
				xxid: '',
				xxList: [],
				xxpage: 1,
				xxpageSize: 10,
				xxtotal: 0,
			}
		},
		onLoad() {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
			
			this.tztotal = 0;
			this.tzpage = 1;
			this.tzList = [];
			this.gettzList(this.tzpage, this.tzpageSize);
		},
		onReachBottom() {
			if(this.attrIndex == 1){
				if (this.tztotal != this.tzList.length) {
					this.tzpage = this.tzpage + 1;
						
					this.gettzList(this.tzpage, this.tzpageSize);
				}
			}else{
				if (this.xxtotal != this.xxList.length) {
					this.xxpage = this.xxpage + 1;
						
					this.getxxList(this.xxpage, this.xxpageSize);
				}
			}
		},
		methods:{
			//通知列表
			gettzList(page, pageSize){
				this.request({
					url: '/xcx/noticelist',
					data: {
						agent_id: uni.getStorageSync('agent_id'),
						page: page,
						pageSize: pageSize,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						this.tztotal = res.data.data.total;
						this.tzList = this.tzList.concat(res.data.data.data);
					} else {
						uni.showToast({
							title: '请求失败，请稍后再试',
							icon: 'none'
						})
					}
				})
			},
			//查看通知公告详情
			chakantz(index){
				this.tzid = this.tzList[index].id;
				let that = this;
				
				if(that.tzList[index].dqstatus == 0){
					//更新读取状态
					this.request({
						url: '/xcx/noticestatus',
						data: {
							agent_id: uni.getStorageSync('agent_id'),
							article_id: this.tzid,
						},
						method: "POST"
					}).then(res => {
						if (res.data.code == 200) {
							that.tzList[index].dqstatus = 1;
						} else {
							uni.showToast({
								title: '请求失败，请稍后再试',
								icon: 'none'
							})
						}
					})
				}
			},
			//消息列表
			getxxList(page, pageSize){
				this.request({
					url: '/xcx/messagelist',
					data: {
						agent_id: uni.getStorageSync('agent_id'),
						role_id: uni.getStorageSync('role_id'),
						page: page,
						pageSize: pageSize,
					},
					method: "POST"
				}).then(res => {
					if (res.data.code == 200) {
						this.xxtotal = res.data.data.total;
						this.xxList = this.xxList.concat(res.data.data.data);
					} else {
						uni.showToast({
							title: '请求失败，请稍后再试',
							icon: 'none'
						})
					}
				})
			},
			//查看消息详情
			chakanxx(index){
				this.xxid = this.xxList[index].id;
				let that = this;
				
				if(that.xxList[index].dqstatus == 0){
					//更新读取状态
					this.request({
						url: '/xcx/messagestatus',
						data: {
							agent_id: uni.getStorageSync('agent_id'),
							message_id: this.xxid,
							role_id: uni.getStorageSync('role_id'),
						},
						method: "POST"
					}).then(res => {
						if (res.data.code == 200) {
							that.xxList[index].dqstatus = 1;
						} else {
							uni.showToast({
								title: '请求失败，请稍后再试',
								icon: 'none'
							})
						}
					})
				}
			},
			qbduqu(){
				let attrIndex = this.attrIndex;
				let that = this;
				if(attrIndex == 1){
					//通知公告
					this.request({
						url: '/xcx/noticestatus',
						data: {
							agent_id: uni.getStorageSync('agent_id'),
							article_id: '',
						},
						method: "POST"
					}).then(res => {
						if (res.data.code == 200) {
							that.tztotal = 0;
							that.tzpage = 1;
							that.tzList = [];
							that.gettzList(that.tzpage, that.tzpageSize);
						} else {
							uni.showToast({
								title: '请求失败，请稍后再试',
								icon: 'none'
							})
						}
					})
				}else{
					this.request({
						url: '/xcx/messagestatus',
						data: {
							agent_id: uni.getStorageSync('agent_id'),
							role_id: uni.getStorageSync('role_id'),
						},
						method: "POST"
					}).then(res => {
						if (res.data.code == 200) {
							that.xxtotal = 0;
							that.xxpage = 1;
							that.xxList = [];
							that.getxxList(that.xxpage, that.xxpageSize);
						} else {
							uni.showToast({
								title: '请求失败，请稍后再试',
								icon: 'none'
							})
						}
					})
				}
			},
			qiehuan(index){
				this.attrIndex = index;
				if(this.attrIndex == 1){
					this.tztotal = 0;
					this.tzpage = 1;
					this.tzList = [];
					this.gettzList(this.tzpage, this.tzpageSize);
				}else{
					this.xxtotal = 0;
					this.xxpage = 1;
					this.xxList = [];
					this.getxxList(this.xxpage, this.xxpageSize);
				}
			},
			fanhui(){
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #f9f9ff;
	}
	
	.message {
		width: 100%;
		margin: 0 auto;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;
			
				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
			padding: 10rpx 40rpx 0;
			box-sizing: border-box;
			
			.left{
				.mess{
					height: 48rpx;
					line-height: 48rpx;
					text-align: center;
					
					view{
						font-size: 24rpx;
						color: #333333;
					}
					
					.dian{
						width: 8rpx;
						height: 8rpx;
						background: #5C88FA;
						display: block;
						border-radius: 50%;
						margin: 0 auto;
					}
					
					.active{
						font-size: 32rpx;
						color: #333333;
						font-weight: 700;
						margin-left: 0;
					}
					
					.active1{
						margin-left: 0;
						margin-right: 20rpx;
					}
					
					.active2{
						margin-left: 20rpx;
					}
				}
			}
			
			.right{
				width: 130rpx;
				height: 40rpx;
				line-height: 40rpx;
				text-align: center;
				font-size: 24rpx;
				color: #333333;
				background: #FFFFFF;
				border-radius: 24rpx;
				margin-top: 4rpx;
			}
		}
		
		.nr{
			width: 90%;
			margin: 0 auto;
			z-index: 70;
			margin-top: -210rpx;
			
			.list{
				width: 100%;
				background: #fff;
				border-radius: 20rpx;
				margin-bottom: 20rpx;
				padding: 26rpx;
				
				.top{
					width: 100%;
					border-bottom: 1rpx solid #F1F1F1;
					padding-bottom: 4rpx;
					
					.shang{
						
						image{
							width: 72rpx;
							height: 72rpx;
							margin-right: 20rpx;
						}
						
						.title{
							font-size: 28rpx;
							color: #333333;
							margin-top: 22rpx;
						}
						
						.time{
							font-size: 24rpx;
							color: #CACACA;
							margin-top: 22rpx;
							margin-right: 8rpx;
						}
					}
					
					.xia{
						text-align: right;
						margin-right: 8rpx;
						font-size: 24rpx;
						color: #CACACA;
					}
				}
				
				.bottom{
					width: 96%;
					margin: 0 auto;
					margin-top: 20rpx;
					position: relative;
					
					.name{
						width: 90%;
						font-size: 24rpx;
						color: #333333;
						float: left;
						line-height: 36rpx;
					}
					
					.desc{
						width: 90%;
						font-size: 24rpx;
						color: #333333;
						line-height: 36rpx;
						float: left;
					}
					
					.image{
						width: 30rpx;
						height: 30rpx;
						float: right;
					}
					
					.shouqi{
						position: absolute;
						right: 0;
						top: 0;
						width: 30rpx;
						height: 30rpx;
					}
				}
			}
		}
		
		.empty{
			width: 100%;
			margin: 20rpx auto 0;
			text-align: center;
			
			image{
				width: 390rpx;
				height: 340rpx;
			}
			
			text{
				display: block;
				margin-top: 60rpx;
				font-size: 24rpx;
				color: #666666;
			}
		}
	}
</style>